﻿using System;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using System.IO;

namespace FilesNameCopier
{
  public partial class OptionsForm : Form
  {
    string desktopLink;
    string sendToLink;

    public OptionsForm()
    {
      InitializeComponent();
    }

    private void OptionsForm_Load(object sender, EventArgs e)
    {
      string lnk = Application.ProductName + ".lnk";
      desktopLink = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), lnk);
      sendToLink = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SendTo), lnk);

      topMostCheckBox.Checked = FilesNameCopier.Properties.Settings.Default.TopMost;
      openNotepadCheckBox.Checked = FilesNameCopier.Properties.Settings.Default.OpenNotepad;
      saveDialogCheckBox.Checked = FilesNameCopier.Properties.Settings.Default.SaveDialog;

      dateTimeCheckBox.Checked = FilesNameCopier.Properties.Settings.Default.CaptureFileDateTime;
      sizeCheckBox.Checked = FilesNameCopier.Properties.Settings.Default.CaptureFileSize;

      string modeName = FilesNameCopier.Properties.Settings.Default.DefaultCapture.ToLower();
      if (modeName.IndexOf("long") != -1)
        defaultCaptureComboBox.SelectedIndex = 1; //Long
      else if (modeName.IndexOf("short") != -1)
        defaultCaptureComboBox.SelectedIndex = 2; //Short
      else
        defaultCaptureComboBox.SelectedIndex = 0; //Full

      SetShortcutButtons();
    }

    private void OptionsForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      FilesNameCopier.Properties.Settings.Default.TopMost = topMostCheckBox.Checked;
      FilesNameCopier.Properties.Settings.Default.DefaultCapture = defaultCaptureComboBox.SelectedItem.ToString();
      FilesNameCopier.Properties.Settings.Default.OpenNotepad = openNotepadCheckBox.Checked;
      FilesNameCopier.Properties.Settings.Default.SaveDialog = saveDialogCheckBox.Checked;

      FilesNameCopier.Properties.Settings.Default.CaptureFileDateTime = dateTimeCheckBox.Checked;
      FilesNameCopier.Properties.Settings.Default.CaptureFileSize = sizeCheckBox.Checked;

      FilesNameCopier.Properties.Settings.Default.Save();
    }

    private void buttonAbout_Click(object sender, EventArgs e)
    {
      Program.ShowAbout(this);
    }

    private void OptionsForm_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.Modifiers == Keys.None)
      {
        if (e.KeyData == Keys.F1)
        {
          Program.ShowAbout(this);
          e.Handled = true;
        }
      }
    }

    private void SetShortcutButtons()
    {
      if (System.IO.File.Exists(desktopLink))
        desktopLinkLabel.Text = "Remove shortcut from desktop.";
      else
        desktopLinkLabel.Text = "Add shortcut to desktop.";

      if (System.IO.File.Exists(sendToLink))
        sendToLinkLabel.Text = "Remove shortcut from 'Send to' menu.";
      else
        sendToLinkLabel.Text = "Add shortcut to 'Send to' menu.";
    }

    private void ProcessShortcut(string linkFile)
    {
      try
      {
        if (System.IO.File.Exists(linkFile))
          System.IO.File.Delete(linkFile);
        else
        {
          WshShell shell = new WshShell();
          IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(linkFile);
          shortcut.Description = Application.ProductName;
          shortcut.TargetPath = Application.ExecutablePath;
          shortcut.Save();
        }
      }
      catch (Exception) { }
      SetShortcutButtons();
    }

    private void linkLabelDesktopLnk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      ProcessShortcut(desktopLink);
    }

    private void linkLabelSendToLnk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      ProcessShortcut(sendToLink);
    }
  }
}
